﻿Public Class HOSPITAL
    Dim connect As CONDB = CONDB.NewConnection()
    Dim sC As FILTERCLASS
    Dim sJ As FILTERCLASS
    Dim sA As FILTERCLASS
    Dim sT As FILTERCLASS

    Dim PROVIDER_MASHOSPITAL As New PROVIDERSCLASS

    Private Sub FRMHNTxt_KeyPress(sender As Object, e As KeyPressEventArgs)
        e.KeyChar = e.KeyChar.ToString.ToUpper
    End Sub

    Private Sub HOSPITAL_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridMashosp()
        SearchCOUNTRY()
        'SearchCHANGWAT()
        'SearchAMPUR()
        'SearchTAMBON()

        DesignForm(Me)
        DesignGrid(MASHOSPGrid)

        PROVIDER_MASHOSPITAL.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridMashosp()
        Dim objSelectDataMASHOSP As New MASHOSP
        Dim MasHOSP As DataTable
        MasHOSP = connect.GetTable(objSelectDataMASHOSP.selectMashosp_)
        MASHOSPGrid.PrimaryGrid.DataSource = MasHOSP
    End Sub

    Private Sub SearchCOUNTRY()
        Dim sql As String
        sql = "SELECT CAST(codecountry as CHAR(8)) AS CODECOUNTRY,`country` FROM mascountry"
        sC = New FILTERCLASS(CODECOUNTRYTxt, sql, "รหัสประเทศ,ประเทศ", "80,193", "1,1", "1,1")
        sC.SetTagIndex = 0 ' settag
        sC.SetTextIndex = 1 'settext
        sC.SetShowBorder = True
    End Sub

    Private Sub SearchCHANGWAT()
        Dim sql As String
        sql = "SELECT `codechangwat`,`changwat` FROM maschangwat"
        sJ = New FILTERCLASS(CODECHANGWATTxt, sql, "รหัสจังหวัด,จังหวัด", "80,193", "0,1", "0,1")
        sJ.SetTagIndex = 0 ' settag
        sJ.SetTextIndex = 1 'settext
        sJ.SetShowBorder = True
    End Sub

    Private Sub SearchAMPUR()
        Dim sql As String
        sql = "SELECT `ampid`,`ampur` FROM masampur"
        sA = New FILTERCLASS(AMPIDTxt, sql, "รหัสอำเภอ,อำเภอ", "80,193", "0,1", "0,1")
        sA.SetTagIndex = 0 ' settag
        sA.SetTextIndex = 1 'settext
        sA.SetShowBorder = True
    End Sub

    Private Sub SearchTAMBON()
        Dim sql As String
        sql = "SELECT `tmbid`,`tambon` FROM mastambon"
        sT = New FILTERCLASS(TMBIDTxt, sql, "รหัสตำบล,ตำบล", "80,193", "0,1", "0,1")
        sT.SetTagIndex = 0 ' settag
        sT.SetTextIndex = 1 'settext
        sT.SetShowBorder = True
    End Sub

    Private Sub cleardata()
        HOSPIDTxt.Text = ""
        HOSPCODETxt.Text = ""
        HOSPNAMETxt.Text = ""
        FRMHNTxt.Text = ""
        LANGUCODETxt.Text = "'"
        TMBIDTxt.Text = ""
        TMBIDTxt.Enabled = False
        AMPIDTxt.Text = ""
        AMPIDTxt.Enabled = False
        CODECHANGWATTxt.Text = ""
        CODECHANGWATTxt.Enabled = False
        CODECOUNTRYTxt.Text = ""
    End Sub

    Private Sub MASHOSPGrid_CellClick_1(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASHOSPGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("HOSPID").Value() IsNot DBNull.Value Then
            HOSPIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("HOSPID").Value()
        Else
            HOSPIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสสถานบริการ").Value() IsNot DBNull.Value Then
            HOSPCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสสถานบริการ").Value()
        Else
            HOSPCODETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อสถานบริการ").Value() IsNot DBNull.Value Then
            HOSPNAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อสถานบริการ").Value()
        Else
            HOSPNAMETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รูปแบบ HN").Value() IsNot DBNull.Value Then
            FRMHNTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รูปแบบ HN").Value()
        Else
            FRMHNTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสภาษา").Value() IsNot DBNull.Value Then
            LANGUCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสภาษา").Value()
        Else
            LANGUCODETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเทศ").Value() IsNot DBNull.Value Then
            CODECOUNTRYTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเทศ").Value()
        Else
            CODECOUNTRYTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จังหวัด").Value() IsNot DBNull.Value Then
            CODECHANGWATTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จังหวัด").Value()
        Else
            CODECHANGWATTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("อำเภอ").Value() IsNot DBNull.Value Then
            AMPIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("อำเภอ").Value()
        Else
            AMPIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ตำบล").Value() IsNot DBNull.Value Then
            TMBIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ตำบล").Value()
        Else
            TMBIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODECOUNTRY").Value() IsNot DBNull.Value Then
            CODECOUNTRYTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODECOUNTRY").Value()
        Else
            CODECOUNTRYTxt.Tag = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODECHANGWAT").Value() IsNot DBNull.Value Then
            CODECHANGWATTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODECHANGWAT").Value()
        Else
            CODECHANGWATTxt.Tag = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("AMPID").Value() IsNot DBNull.Value Then
            AMPIDTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("AMPID").Value()
        Else
            AMPIDTxt.Tag = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("TMBID").Value() IsNot DBNull.Value Then
            TMBIDTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("TMBID").Value()
        Else
            TMBIDTxt.Tag = ""
        End If

        TMBIDTxt.Enabled = True
        AMPIDTxt.Enabled = True
        CODECHANGWATTxt.Enabled = True
    End Sub

    Private Sub CODECOUNTRYTxt_TextChanged_1(sender As Object, e As EventArgs) Handles CODECOUNTRYTxt.TextChanged
        CODECHANGWATTxt.Enabled = True

        If Convert.ToString(CODECOUNTRYTxt.Tag).Trim = "" Then
            CODECHANGWATTxt.Enabled = False
            Return
        End If
        CODECHANGWATTxt.Enabled = True
        Dim sql As String
        sql = "SELECT CAST(`codechangwat` AS NCHAR(3)) AS `CODECHANGWAT`,`changwat` "
        sql += " FROM maschangwat "
        sql += " left join (select * from mascountry ) as mascountry on maschangwat.`codecountry1` = mascountry.`codecountry` "
        sql += " WHERE `codecountry` = '" & Convert.ToString(CODECOUNTRYTxt.Tag).Trim & "'; "
        sJ = New FILTERCLASS(CODECHANGWATTxt, sql, "รหัสจังหวัด,จังหวัด", "80,193", "1,1", "1,1")
        sJ.SetShowBorder = True
    End Sub

    Private Sub CODECHANGWATTxt_TextChanged_1(sender As Object, e As EventArgs) Handles CODECHANGWATTxt.TextChanged
        AMPIDTxt.Enabled = True

        If Convert.ToString(CODECHANGWATTxt.Tag).Trim = "" Then
            AMPIDTxt.Enabled = False
            Return
        End If
        AMPIDTxt.Enabled = True
        Dim sql As String
        sql = "SELECT CAST(`ampid` AS NCHAR(3)) AS `ampid`,`ampur` "
        sql += " FROM masampur "
        sql += " LEFT JOIN (SELECT * FROM maschangwat ) as maschangwat ON masampur.`codechangwat` = maschangwat.`codechangwat` "
        sql += " WHERE maschangwat.`codechangwat` = '" & Convert.ToString(CODECHANGWATTxt.Tag).Trim & "';"
        sA = New FILTERCLASS(AMPIDTxt, sql, "รหัสอำเภอ,อำเภอ", "80,193", "1,1", "1,1")
        sA.SetShowBorder = True
    End Sub

    Private Sub AMPIDTxt_TextChanged_1(sender As Object, e As EventArgs) Handles AMPIDTxt.TextChanged
        TMBIDTxt.Enabled = True

        If Convert.ToString(CODECHANGWATTxt.Tag).Trim = "" Then
            TMBIDTxt.Enabled = False
            Return
        End If
        TMBIDTxt.Enabled = True
        Dim sql As String
        sql = "SELECT CAST(`tmbid` as NCHAR(3)) AS `TMBID`,`tambon` "
        sql += "FROM mastambon "
        sql += "LEFT JOIN (SELECT * FROM masampur ) as masampur ON mastambon.`ampid` = masampur.`ampid` "
        sql += "WHERE masampur.`ampid` = '" & Convert.ToString(AMPIDTxt.Tag).Trim & "'; "
        sT = New FILTERCLASS(TMBIDTxt, sql, "รหัสตำบล,ตำบล", "80,193", "1,1", "1,1")
        sT.SetShowBorder = True
    End Sub

    Private Sub addMashosp_Click_1(sender As Object, e As EventArgs) Handles addMashosp.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASHOSPITAL.PADD_ = True Then
                If HOSPCODETxt.Text.ToString.Length > 0 And HOSPNAMETxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New MASHOSP
                    If Convert.ToString(TMBIDTxt.Tag).Trim <> "" Then
                        objInsertData.TMBID_ = Convert.ToString(TMBIDTxt.Tag).Trim
                    End If
                    If Convert.ToString(AMPIDTxt.Tag).Trim <> "" Then
                        objInsertData.AMPID_ = Convert.ToString(AMPIDTxt.Tag).Trim
                    End If
                    If Convert.ToString(CODECHANGWATTxt.Tag).Trim <> "" Then
                        objInsertData.CODECHANGWAT_ = Convert.ToString(CODECHANGWATTxt.Tag).Trim
                    End If
                    If Convert.ToString(CODECOUNTRYTxt.Tag).Trim <> "" Then
                        objInsertData.CODECOUNTRY_ = Convert.ToString(CODECOUNTRYTxt.Tag).Trim
                    End If
                    objInsertData.HOSPCODE_ = HOSPCODETxt.Text
                    objInsertData.HOSPNAME_ = HOSPNAMETxt.Text
                    objInsertData.FRMHN_ = FRMHNTxt.Text
                    'objInsertData.DISPYR_ = DISPYRTxt.Text
                    objInsertData.LANGUCODE_ = LANGUCODETxt.Text
                    objInsertData.insertMashosp()

                    SelectDatagridMashosp()

                    cleardata()
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
            Else
                MsgBox(PROVIDER_MASHOSPITAL.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMashosp_Click_1(sender As Object, e As EventArgs) Handles editMashosp.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASHOSPITAL.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
                If editStat = 1 Then
                    Dim objeditData As New MASHOSP
                    If Convert.ToString(TMBIDTxt.Text).Trim <> "" Then
                        objeditData.TMBID_ = Convert.ToString(TMBIDTxt.Tag).Trim
                    End If
                    If Convert.ToString(AMPIDTxt.Tag).Trim <> "" Then
                        objeditData.AMPID_ = Convert.ToString(AMPIDTxt.Tag).Trim
                    End If
                    If Convert.ToString(CODECHANGWATTxt.Tag).Trim <> "" Then
                        objeditData.CODECHANGWAT_ = Convert.ToString(CODECHANGWATTxt.Tag).Trim
                    End If
                    If Convert.ToString(CODECOUNTRYTxt.Tag).Trim <> "" Then
                        objeditData.CODECOUNTRY_ = Convert.ToString(CODECOUNTRYTxt.Tag).Trim
                    End If
                    objeditData.HOSPID_ = HOSPIDTxt.Text
                    objeditData.HOSPCODE_ = HOSPCODETxt.Text
                    objeditData.HOSPNAME_ = HOSPNAMETxt.Text
                    objeditData.FRMHN_ = FRMHNTxt.Text
                    objeditData.LANGUCODE_ = LANGUCODETxt.Text
                    objeditData.editMashosp()

                    SelectDatagridMashosp()

                    cleardata()
                End If
            Else
                MsgBox(PROVIDER_MASHOSPITAL.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(mashosp.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(mashosp.Text)
        nextform.ShowDialog()
    End Sub

    
    Private Sub CODECOUNTRYTxt_Click(sender As Object, e As EventArgs) Handles CODECOUNTRYTxt.Click
        CODECOUNTRYTxt.Text = ""
    End Sub

    Private Sub CODECHANGWATTxt_Click(sender As Object, e As EventArgs) Handles CODECHANGWATTxt.Click
        CODECHANGWATTxt.Text = ""
    End Sub

    Private Sub AMPIDTxt_Click(sender As Object, e As EventArgs) Handles AMPIDTxt.Click
        AMPIDTxt.Text = ""
    End Sub

    Private Sub TMBIDTxt_Click(sender As Object, e As EventArgs) Handles TMBIDTxt.Click
        TMBIDTxt.Text = ""
    End Sub
End Class